//
//  shoujianxqViewController.m
//  BAOASystem
//
//  Created by michael on 2018/2/23.
//  Copyright © 2018年 czd. All rights reserved.
//

#import "shoujianxqViewController.h"

@interface shoujianxqViewController ()

@end

@implementation shoujianxqViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:

[UIColor whiteColor],NSForegroundColorAttributeName, nil];
 
[self.navigationController.navigationBar setTitleTextAttributes:attributes];
 self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"0a62ea"];
 self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
 self.view.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
 [self loaddata];
 
    // Do any additional setup after loading the view.
}
- (void)loaddata{
 [MBProgressHUD showHUDAddedTo:self.view animated:YES];

NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><id>%@</id><msgtype>1</msgtype></body></message>",userName,password,website,language,BAtype,_ipid];
 bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bMessageList];
 [HttpTools requestWithURL:path arrays:array method:@"messageDetail" success:^(id JSON) {
   NSLog(@"%@",JSON);
   
[self initview];
[_tableView reloadData];
_dic = JSON[@"message"];
 } failure:^(NSError *error) {

    }];

}
- (void)initview{

if(!_tableView){
     _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth,KScreenHeight-kTopHeight) style:UITableViewStylePlain];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
 self.tableView.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
_tableView.bounces = NO;
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];

}

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row ==0){
static NSString *cellid0 = @"cell0";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid0];
     if (!cell) {
       cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid0];
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
      cell.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, KScreenWidth-20, 30)];
 label.textAlignment = 0;
  label.text = _dic[@"fromCompanyName"];
  label.backgroundColor = [UIColor whiteColor];
  label.textColor = [UIColor colorWithHexString:@"484848"];
  label.font = [UIFont systemFontOfSize:14];
 [cell.contentView addSubview:label];
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(10, label.bottom, KScreenWidth-20, 30)];
//view1.backgroundColor = [UIColor colorWithHexString:@"aeaeae"];
view1.backgroundColor = [UIColor whiteColor];
[cell.contentView addSubview:view1];
UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(5, 0, KScreenWidth-30, .5)];
view2.backgroundColor = [UIColor colorWithHexString:@"aeaeae"];
[view1 addSubview:view2];
UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(5, 29.5, KScreenWidth-30, .5)];
view3.backgroundColor = [UIColor colorWithHexString:@"aeaeae"];
[view1 addSubview:view3];
 UILabel *namelabel = [[UILabel alloc]initWithFrame:CGRectMake(10,.5, 80, 29)];
 namelabel.textAlignment = 0;
  namelabel.text = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"发件人:", nil),_dic[@"fromUserName"]];
  namelabel.textColor = [UIColor colorWithHexString:@"aeaeae"];
  namelabel.font = [UIFont systemFontOfSize:12];
 [view1 addSubview:namelabel];
 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
 button.backgroundColor = [UIColor colorWithHexString:@"0aa0f6"];
 [button setTitle:NSLocalizedString(@"查看联系方式", nil) forState:UIControlStateNormal];
  [button addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];

 button.frame = CGRectMake(namelabel.right+5, 5, 80, 20);
   button.titleLabel.font = [UIFont systemFontOfSize:12];
 [view1 addSubview:button];
  UILabel *timelabel = [[UILabel alloc]initWithFrame:CGRectMake(view1.right-90,.5, 80, 29)];
 timelabel.textAlignment = 2;
  timelabel.text = _dic[@"createDate"];
  timelabel.textColor = [UIColor colorWithHexString:@"aeaeae"];
  timelabel.font = [UIFont systemFontOfSize:12];
 [view1 addSubview:timelabel];
 NSString *string =_dic[@"content"];
  CGFloat height =  [string boundingRectWithSize:CGSizeMake(KScreenWidth - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size.height;
  if (height < 80) {
            height = 80;
        }
 UILabel *textlabel = [[UILabel alloc]initWithFrame:CGRectMake(10, view1.bottom, KScreenWidth-20,height)];
 textlabel.textAlignment = 0;
 textlabel.numberOfLines = 0;
 textlabel.backgroundColor = [UIColor whiteColor];
  textlabel.text = string;
  textlabel.textColor = [UIColor colorWithHexString:@"484848"];
  textlabel.font = [UIFont systemFontOfSize:13];
 [cell.contentView addSubview:textlabel];
 
       }
return cell;

}
else{
static NSString *cellid1 = @"cell1";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid1];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
     if (!cell) {
  cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid1];
      cell.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
 _textview = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, KScreenWidth-20, 100)];
  [cell.contentView addSubview:_textview];
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
      button2.frame = CGRectMake(10, _textview.bottom+10, KScreenWidth-20, 40);
          button2.backgroundColor = [UIColor colorWithHexString:@"0a62ea"];

//        [button2.layer setBorderColor:[UIColor blueColor].CGColor];
//    [button2.layer setBorderWidth:1];
// [button2.layer setMasksToBounds:YES];
  [button2 setTitle:NSLocalizedString(@"发送信息", nil) forState:UIControlStateNormal];
    button2.titleLabel.font = [UIFont systemFontOfSize: 14.0];
   [button2 setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
//        button2.layer.cornerRadius = button.height/2;
 [button2 addTarget:self action:@selector(action2) forControlEvents:UIControlEventTouchUpInside];

            [cell.contentView addSubview:button2];
  }
  return cell;
  
}

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0){
 NSString *string =_dic[@"content"];
  CGFloat height =  [string boundingRectWithSize:CGSizeMake(KScreenWidth - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size.height;
  if (height < 80) {
            height = 80;
        }
return height+10+60;
}else{
return 170;
}

}
- (void)action{
NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><uid>%@</uid></body></message>",userName,password,website,language,BAtype,_dic[@"fromMemberId"]];
 bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bMessageList];
 [HttpTools requestWithURL:path arrays:array method:@"msgUserInfo" success:^(id JSON) {
   NSLog(@"%@",JSON);
if([[NSString stringWithFormat:@"%@",JSON[@"code"]]isEqualToString:@"200"]){
_dict = JSON;
[self initlitview];

}else{

    [self.view makeToast:JSON[@"msg"]];

}

 } failure:^(NSError *error) {

    }];

}
- (void)initlitview{
_view1 = [[UIView alloc]initWithFrame:CGRectMake(KScreenWidth/2-155, 100, 300, 120)];
_view1.backgroundColor = [UIColor whiteColor];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, self.view1.width-20, 20)];
label3.textColor = [UIColor colorWithHexString:@"484848"];
    label3.font = [UIFont systemFontOfSize: 14.0];
label3.text =[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"手机号码:", nil),_dict[@"userPhone"]];
label3.textAlignment = 1;
label3.backgroundColor = [UIColor clearColor];
[_view1 addSubview:label3];
UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(10, label3.bottom+10, self.view1.width-20, 20)];
    label4.font = [UIFont systemFontOfSize: 14.0];

label4.textColor = [UIColor colorWithHexString:@"484848"];
label4.text =[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"电子邮箱:", nil),_dict[@"userEmail"]];
label4.textAlignment = 1;
label4.backgroundColor = [UIColor clearColor];
[_view1 addSubview:label4];

 UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
      button4.frame = CGRectMake(_view1.width/2-30, label4.bottom+10, 60, 30);
//          button3.backgroundColor = [UIColor colorWithHexString:@"0a62ea"];

button4.backgroundColor = [UIColor colorWithHexString:@"0aa0f6"];
    [button4 setTitle:NSLocalizedString(@"我知道了", nil) forState:UIControlStateNormal];
    button4.titleLabel.font = [UIFont systemFontOfSize: 13.0];
          [button4 setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
//        button2.layer.cornerRadius = button.height/2;
                [button4 addTarget:self action:@selector(action4) forControlEvents:UIControlEventTouchUpInside];

            [_view1 addSubview:button4];
[self.view addSubview:_view1];


}
- (void)action4{
[_view1 removeFromSuperview];


}
- (void)action2{
NSString *string=[_textview.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
 if ([string isEqualToString:@""]|| string.length == 0){
     [self.view makeToast:NSLocalizedString(@"请填写回复内容", nil)];
}else{
NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
NSString *companyName = [[NSUserDefaults standardUserDefaults] objectForKey:@"companyName"];
NSString *uname = [[NSUserDefaults standardUserDefaults] objectForKey:@"uname"];
NSString *phone = [[NSUserDefaults standardUserDefaults] objectForKey:@"phone"];
NSString *email = [[NSUserDefaults standardUserDefaults] objectForKey:@"email"];

   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
   NSString *productId =_dic[@"productId"];
   if (productId.length>0) {
    
}else{

productId = @"";
}
NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><messageinfo><toMemberCompanyId>%@</toMemberCompanyId><toMemberNameId>%@</toMemberNameId><productId>%@</productId><title>%@</title><content>%@</content><fromMemberId>%@</fromMemberId><fromCompanyId>%@</fromCompanyId><fromCompanyName>%@</fromCompanyName><fromMemberName>%@</fromMemberName><formMemberPhone>%@</formMemberPhone><formMemberEmail>%@</formMemberEmail></messageinfo></body></message>",userName,password,website,language,BAtype,_dic[@"fromCompanyId"],_dic[@"fromMemberId"],productId,_dic[@"title"],string,_dic[@"toMemberNameId"],_dic[@"toMemberCompanyId"],companyName,uname,phone,email];
 bodyStr= [bodyStr URLEncodedString];
 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bMessageList];
 [HttpTools requestWithURL:path arrays:array method:@"messageSave" success:^(id JSON) {
 if([[NSString stringWithFormat:@"%@",JSON[@"code"]]isEqualToString:@"200"]){
          [self.view makeToast:JSON[@"msg"]];

 [self performSelector: @selector(popaction) withObject: nil afterDelay:1.0f];

 
 }else{
     [self.view makeToast:JSON[@"msg"]];

 }
  

//[_tableView reloadData];
 } failure:^(NSError *error) {

    }];

}

}
- (void)popaction{
[self.navigationController popViewControllerAnimated:YES];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
